//
//  InformationView.m
//  LoveTravel
//
//  Created by MS on 16-1-26.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//

#import "InformationView.h"
#import "InfomationCell.h"
#import "InformationHeaderView.h"
@interface InformationView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,weak) UITableView * tableView;



@end

@implementation InformationView

-(void)setProductData:(ProductDetailModel *)productData
{
    _productData = productData;
    
    
    [self.tableView reloadData];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        UITableView * tableView  = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStyleGrouped];
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 100;
        self.tableView = tableView;
        [self addSubview:tableView];
        tableView.showsVerticalScrollIndicator = NO;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.scrollEnabled = NO;
        
//        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger num ;
    switch (section) {
        case 0:
            num = self.productData.expense.count;
            break;
        case 1:
            num =1;
            break;
        case 2:
            num = 2;
            break;
//        case 3:
//            num = 1;
//            break;
        default:
            break;
    }
       return num;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InfomationCell * cell = [InfomationCell cellWithtableView:tableView];
    cell.indexPath = indexPath;
    switch (indexPath.section) {
        case 0:{
          Product_Expense * expensr = self.productData.expense[indexPath.row];
            if (expensr.content.count>0) {
                NSString * text = [NSString stringWithFormat:@"%@\n⋆ %@",expensr.group,expensr.content[0]];
                cell.textView.text = text;
            }
        }
            break;
        case 1:
            cell.textView.text = self.productData.schedule;
            break;
        case 2:
            if (indexPath.row==0) {
                cell.textView.text = _productData.contact;
            }else{
                
                if (_productData.order_url.length>0) {
                    
                    NSRange range = [_productData.order_url rangeOfString:@".com"];
                    if (range.location == NSNotFound) {
                        range = [_productData.order_url rangeOfString:@".cn"];
                    }
                    
                    if (range.location == NSNotFound) {
                         cell.textView.text =[NSString stringWithFormat:@"在线预订：%@", _productData.order_url];
                    }else{
                        cell.textView.text =[NSString stringWithFormat:@"在线预订：%@", [_productData.order_url substringToIndex:range.location+range.length]];
                    }
                }
            }
            
            break;
//        case 3:
//             cell.textView.text = @"13";
//            break;
        default:
            break;
    }

    return cell;
}



-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    InformationHeaderView * baseView = [InformationHeaderView infoHeaderViewWithXib];
    
    switch (section) {
        case 0:
            baseView.titleView.text = @"费用";
            baseView.subTitleView.text = [NSString stringWithFormat:@"%@元",_productData.price];
            break;
        case 1:
            baseView.titleView.text = @"时间";
            break;
        case 2:
            baseView.titleView.text = @"预定方式";
            break;
        default:
            break;
    }
    
    return baseView;
}



-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    
    
    UIView * view = nil;
    if (section==2) {
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.width, 20)];
        label.textColor = [UIColor grayColor];
        label.font = [UIFont systemFontOfSize:11];
        label.backgroundColor = [UIColor whiteColor];
        label.text = [NSString stringWithFormat:@"  本活动由%@提供",_productData.owner.username];
        view = label;
    }
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==2) {
        return 20;
    }
    return 0.01;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}

@end
